uniform sampler2D myTexture1;
uniform sampler2D myTexture2;

varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;

void main()
{
    vec3 n,halfV;
    float NdotL,NdotHV;
    
    /* O termo ambiente sempre estará presente */
    vec4 color = ambient;
    
    /* calcula a normaldistorcida pela cor da textura */
    n = 2.0 * (texture2D(myTexture1, gl_TexCoord[0]).rgb - 0.5);
    n = normalize(n+normal);
    
    /* faz o produto escalar entre a normal e a direcao */
    NdotL = max(dot(n,lightDir),0.0);

    /* calcula a cor final do fragmento */
    if (NdotL > 0.0) {
        color += diffuse * NdotL;
        halfV = normalize(halfVector);
        NdotHV = max(dot(n,halfV),0.0);
        color += gl_FrontMaterial.specular *
                gl_LightSource[0].specular *
                pow(NdotHV, gl_FrontMaterial.shininess);
    }
    
    // pega a cor na textura e combina com a cor calculada ateh aqui
    vec4 texval2 = texture2D(myTexture2, vec2(gl_TexCoord[0]));
    color/=2.0;
    color += texval2/2.0;

    gl_FragColor = color;
}